/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import org.crystalrange.model.MenuFolder;
import org.crystalrange.service.MenuFolderService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class MenuFolderSelectController extends GenericForwardComposer {

    protected MenuFolderService menuFolderService;
    protected MenuFolder selectedMenuFolder;
    protected AnnotateDataBinder binder;
    protected Search searchMenuFolder;
    protected List<MenuFolder> menuFolders;

    public MenuFolderService getMenuFolderService() {
        return this.menuFolderService;
    }

    public void setMenuFolderService(MenuFolderService menuFolderService) {
        this.menuFolderService = menuFolderService;
    }

    public MenuFolder getSelectedMenuFolder() {
        return this.selectedMenuFolder;
    }

    public void setSelectedMenuFolder(MenuFolder selectedMenuFolder) {
        this.selectedMenuFolder = selectedMenuFolder;
    }

    public List<MenuFolder> getMenuFolders() {
        return this.menuFolders;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchMenuFolder = new Search(MenuFolder.class);
        this.searchMenuFolder.setMaxResults(100);
        this.searchMenuFolder.addFetch("createdBy");
        this.searchMenuFolder.addFetch("updatedBy");
        this.menuFolders = this.menuFolderService.search(this.searchMenuFolder);

        component.setAttribute("controller", this);
    }

    public void onClick$buttonOk(Event event) throws Exception {
        if (this.selectedMenuFolder != null) {
            this.execution.setAttribute("selectedMenuFolder", this.selectedMenuFolder);
            this.execution.setAttribute("result", "ok");
            this.self.detach();
        } else {
            Messagebox.show("Please select a Menu Folder.");
        }
    }

    public void onClick$buttonCancel(Event event) throws Exception {
        this.execution.setAttribute("selectedMenuFolder", null);
        this.execution.setAttribute("result", "cancel");
        this.self.detach();
    }

    public void onClick$buttonSearch(Event event) throws Exception {
        this.execution.setAttribute("searchMenuFolder", this.searchMenuFolder);

        Window window = (Window) this.execution.createComponents("~./zul/html/menuFolderSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchMenuFolder = (Search) this.execution.getAttribute("searchMenuFolder");
        }

        this.menuFolders = this.menuFolderService.search(this.searchMenuFolder);
        this.binder.loadAll();
    }
}
